<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      id="homeSetting"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      v-model:selectedKeys="selectedKeys"
      :pagination="false"
      @sorter-change="onSorterChange"
      @refresh="getTableData"
    >
      <template #custom-extra>
        <a-button type="primary" size="small" @click="onAddOrEdit(false)">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
      </template>
      <template #invisible="{ record }">
        <template v-if="record.invisible || record.invisible == 0">
          <a-switch
            :checked-value="1"
            :unchecked-value="0"
            v-model="record.invisible"
            :before-change="handleChangeStatus.bind(null, record, 'invisible')"
          ></a-switch>
        </template>
        <template v-else>--</template>
      </template>
      <template #action="{ record }">
        <template v-if="record.invisible || record.invisible == 0">
          <a-button type="text" size="small" @click="onAddOrEdit(record)"> 编辑 </a-button>
          <a-button type="text" size="small" @click="onDeleteBatch(record.id)"> 删除 </a-button>
        </template>
        <template v-else>--</template>
      </template>
    </GiTable>
  </div>
</template>

<script setup>
import { Modal, Message } from '@arco-design/web-vue'
import { applicationMenuList, applicationMenuEdit, applicationMenuDelete } from '@/apis'
import ImgHoverPreview from '@/components/ImgHoverPreview/index.vue'
import $bus from '@/utils/bus'

defineOptions({ name: 'homeSetting' })
const router = useRouter()
const columns = ref([
  {
    title: '应用名称',
    width: 120,
    dataIndex: 'name'
  },
  {
    title: '图标',
    width: 80,
    render: (data) => {
      const url = data.record.path
      return h(ImgHoverPreview, { url, width: 50 })
    }
  },
  {
    title: '路径',
    width: 200,
    render: (data) => {
      const url = data.record.url
      return url || '--'
    }
  },
  {
    title: '是否启用',
    width: 100,
    slotName: 'invisible'
  },
  {
    title: '排序',
    width: 100,
    render: (data) => {
      const url = data.record.sort
      return url || '--'
    }
  },
  {
    title: '操作',
    width: 100,
    fixed: 'right',
    align: 'center',
    slotName: 'action'
  }
])

const loading = ref(false)
const tableData = ref([])
const selectedKeys = ref([])

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    const res = await applicationMenuList()
    tableData.value = res.data
  } finally {
    loading.value = false
  }
}
getTableData()

// 添加/编辑
const onAddOrEdit = (item) => {
  router.push({
    name: 'homeSettingEdit',
    query: { id: item?.id },
    state: item ? { item: JSON.stringify(item) } : undefined
  })
}

// 改变列表中的切换按钮状态
const handleChangeStatus = async (record, str, newValue) => {
  await new Promise((resolve, reject) => {
    applicationMenuEdit({ ...record, [str]: newValue })
      .then(() => {
        Message.success('操作成功')
        getTableData()
        resolve(true)
      })
      .catch(() => {
        reject()
      })
  })
}

// 删除
const onDeleteBatch = async (id) => {
  Modal.warning({
    title: '提示',
    content: '是否确认删除？',
    hideCancel: false,
    onOk: () => {
      loading.value = true
      applicationMenuDelete({ ids: id })
        .then(() => {
          Message.success('删除成功')
          getTableData()
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}

onMounted(() => {
  $bus.on('homeSetting', () => {
    getTableData()
  })
})

onUnmounted(() => {
  $bus.off('homeSetting')
})
</script>
